﻿using Microsoft.Playwright;

namespace ConsoleApp;

/// <summary>
/// 无头浏览器（Headless Browser)无头浏览器是指没有图形用户界面（GUI）的浏览器，它可以在后台运行，执行与常规浏览器相同的任务（如加载网页、运行JavaScript、操作DOM等），但不会显示可视化的浏览器窗口。
/// 浏览器自动化操作: Playwright,PuppeteerSharp 应用场景: 自动化测试, 网页抓取（爬虫）,截图与PDF生成,性能监控
/// </summary>
public class DemoPlaywright
{
    public static async void Screenshot()
    {
        using var playwright = await Playwright.CreateAsync();
        await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
        {
            Headless = false // 设置为 true 可以隐藏浏览器窗口
        });
 
        var page = await browser.NewPageAsync();
        await page.GotoAsync("https://example.com");
 
        // 截图并保存
        await page.ScreenshotAsync();
 
        Console.WriteLine("截图已保存为 example.png");
    }
}